/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import queenb.net.entitybean.N000;
import queenb.net.entitybean.N300;
import queenb.net.entitybean.N450;
import queenb.web.crm.store.N850Store;
import queenb.web.crm.store.N900Store;
import queenb.web.store.N300Store;
import queenb.web.store.N450Store;
import queenb.web.util.Util;

/**
 *
 * @author Nguyen Hung
 */
public class N450Controller extends QueenbController {

    private int pn450;
    private String pv451;
    private String pv452;
    private String pv453;
    private String nv852;
    private String nv303;
    private Date nd304;
    private Date nd305;
    private N450Store n450Store;
    private N300Store n300Store;
    private int actionReturnCode;

    public N450Controller() {
        n450Store = new N450Store();
        n300Store = new N300Store();

        Object n450Object = getRequest().getParameter("pn450");
        if (n450Object != null) {
            pn450 = Util.parseInt(n450Object);
            getDetailEditN450(pn450);
        }
    }

    public void updateN450() {
        try {
            int row = n450Store.updateN450(pn450, pv451, pv452, pv453, getLogin().getModule(), getLogin().getCompanyNo(), getLogin().getUserName());
            if (row > 0) {
                showGlobalReport(true, getResourceString("datasavesuccessful"));
            } else {

                showGlobalReport(true, getResourceString("datasuccessfail"));
            }
            sendRedirectUrlTab("setting.jsp?tab=header14&link14="+Util.convertMD5(getRequest().getRequestURI()));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public List<N300> getListN300L() {
        try {
            List<N300> list = n300Store.getListN300L(getLogin().getModule(), getLogin().getCompanyNo(), this.pn450, getLogin().getUserName());
            return list;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    public List<N300> getListN300R() {
        try {
            List<N300> list = n300Store.getListN300R(getLogin().getModule(), getLogin().getCompanyNo(), this.pn450, getLogin().getUserName());
            return list;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }

    public List<N450> getListAllN450() {

        try {

            List<N450> list = n450Store.getListN450(getLogin().getModule(), getLogin().getCompanyNo(), getLogin().getUserName());
//            Iterator ic = list.iterator();
//            while(ic.hasNext()){
//                N450 n450 = (N450)ic.next();
//                N850Store n850Store = new N850Store();
//            }
            return list;
        } catch (Exception ex) {
            System.out.println(ex);
            return null;
        }

    }

    public List<N450> getListCombN450() {
        try {
            List<N450> listCombN450 = n450Store.findAllN450ForCombo(getLogin().getModule(), 99, getLogin().getUserName());
            return listCombN450;
        } catch (Exception e) {
           return null;
        }

    }

    public void getDetailEditN450(int id) {
        try {
            List<N450> listN850Edit = getListAllN450();
            if (listN850Edit.size() > 0) {
                for (int i = 0; i < listN850Edit.size(); i++) {
                    if (listN850Edit.get(i).getPn450() == id) {
                        this.pv451 = listN850Edit.get(i).getNv451();
                        this.pv453 = listN850Edit.get(i).getNv453();
                        break;
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println("Error when getDetailEditN850. ERROR CODE: " + ex.getMessage());
        }
    }

    public int getActionN450andN850() {
        Object listIdN900 = getRequest().getParameter("idemploy");
        try {
            n300Store.updateN300(6, 8, null, null, null, null, getLogin().getUserName());
        } catch (Exception e) {
            System.out.println("Loi gi day : " + e.getMessage());
        }

        Object pn450Object = getRequest().getParameter("pn450");
        if (listIdN900 != null && pn450Object != null && !listIdN900.toString().equals("")) {
            ArrayList<Integer> listIdN900Arr = new ArrayList<Integer>();
            String[] listIdN900String = listIdN900.toString().split("_");
            for (int i = 0; i < listIdN900String.length; i++) {
                System.out.println("00000000000000" + listIdN900String[i].toString());
                listIdN900Arr.add(Util.parseInt(listIdN900String[i].toString()));
            }
            if (listIdN900Arr.size() > 0) {
                saveEmployeeToGroup(listIdN900Arr, Util.parseInt(pn450Object.toString()));
            }
        }
        if (listIdN900.toString().equals("")) {
            ArrayList<Integer> listIdN900Arr = new ArrayList<Integer>();

            if (listIdN900Arr.size() == 0) {
                         deleteAllN900(listIdN900Arr,Util.parseInt(pn450Object.toString()));
            }

        }
        return actionReturnCode;

    }

    public void saveEmployeeToGroup(ArrayList<Integer> listIdEmployee, int groupEmployee) {
        //Chon ra nhung id nao ma khong co trong list id dc submit len de xoa:
        deleteEmployeeFromGroup(listIdEmployee, groupEmployee);
        //Chon ra nhung id trong list id dc submit len, ma khong co trong db thi tien hanh insert
        insertEmployee(listIdEmployee, groupEmployee);

    }

    public void deleteEmployeeFromGroup(ArrayList<Integer> listIdEmployee, int groupEmployee) {
        System.out.println("$$$$$$$$$$$$$$$$$$$groupEmployee ne: " + groupEmployee);
        List<N300> listEmployeeInGroup = null;
        n300Store = new N300Store();
        try {
            listEmployeeInGroup = n300Store.getListN300R(getLogin().getModule(), 99, groupEmployee, getLogin().getUserName());

        } catch (Exception ex) {
            System.out.println("Error when get employee in group. ERROR CODE: " + ex.getMessage());
        }
        if (listEmployeeInGroup != null) {
            for (int i = 0; i < listEmployeeInGroup.size(); i++) {
                if (!listIdEmployee.contains(listEmployeeInGroup.get(i).getPn850())) {
                    try {
                        // getN900Store().deleteN900(listEmployeeInGroup.get(i).getPn100(), groupEmployee, getLogin().getUserName());
                        n450Store.deleteN300(listEmployeeInGroup.get(i).getPn850(), groupEmployee, getLogin().getUserName());
                    } catch (Exception ex) {
                        System.out.println("Error when delete employee in group. ERROR CODE: " + ex.getMessage());
                    }
                }
            }
        }
    }

    //Xoa het tat cac cac nhan vien thuoc nhom
    public void deleteAllN900(ArrayList<Integer> listIdEmployee, int groupEmployee) {
        List<N300> listEmployeeInGroup = null;
        n300Store = new N300Store();
        try {
            listEmployeeInGroup = n300Store.getListN300R(getLogin().getModule(), 99, groupEmployee, getLogin().getUserName());

        } catch (Exception ex) {
            System.out.println("Error when get employee in group. ERROR CODE: " + ex.getMessage());
        }
        if (listEmployeeInGroup != null) {
            for (int i = 0; i < listEmployeeInGroup.size(); i++) {
                if (!listIdEmployee.contains(listEmployeeInGroup.get(i).getPn850())) {
                    try {

                        n450Store.deleteN300(listEmployeeInGroup.get(i).getPn850(), groupEmployee, getLogin().getUserName());
                    } catch (Exception ex) {
                        System.out.println("Error when delete employee in group. ERROR CODE: " + ex.getMessage());
                    }
                }
            }
        }
        try {
            System.out.println("delete all with group: " + groupEmployee);
            n450Store.deleteN300(0, groupEmployee, getLogin().getUserName());
        } catch (Exception ex) {
            System.out.println("Error when delete All N900. ERROR CODE: " + ex.getMessage());
        }

    }

    public void insertEmployee(ArrayList<Integer> listIdEmployee, int groupEmployee) {
        List<N300> listEmployeeInGroup = null;
        n300Store = new N300Store();
        try {
            listEmployeeInGroup = n300Store.getListN300R(getLogin().getModule(), 99, groupEmployee, getLogin().getUserName());

        } catch (Exception ex) {
            System.out.println("Error when get employee in group. ERROR CODE: " + ex.getMessage());
        }
        //Truong hop trong database chua to nhom nao thuoc bo phan
        if (listEmployeeInGroup.size() == 0) {


            for (int i = 0; i < listIdEmployee.size(); i++) {
                try {
                    // getN900Store().updateN900(listIdEmployee.get(i), groupEmployee, null, null, null, null, getLogin().getUserName());
                    n450Store.updateN450i(groupEmployee, Util.parseInt(listIdEmployee.get(i)), null, null, null, null, getLogin().getUserName());
                } catch (Exception ex) {
                    System.out.println("Error qqqqq when insert employee with Id= " + listIdEmployee.get(i) + " and groupEmployee="
                            + groupEmployee + ". ERROR CODE: " + ex.getMessage());
                }
            }
        } //Truong hop chon nhung id trong list id dc su    bmit len de save vao
        else {
            ArrayList<Integer> listIdInGroup = new ArrayList<Integer>();
            for (int i = 0; i < listEmployeeInGroup.size(); i++) {
                listIdInGroup.add(listEmployeeInGroup.get(i).getPn850());
            }
            System.out.println("---------size" + listIdEmployee.size());
            for (int i = 0; i < listIdEmployee.size(); i++) {
                if (!listIdInGroup.contains(listIdEmployee.get(i))) {
                    try {
                        // getN900Store().updateN900(listIdEmployee.get(i), groupEmployee, null, null, null, null, getLogin().getUserName());
                        System.out.println("000000000000------" + groupEmployee + "," + listIdEmployee.get(i) + "," + null + "," + null + "," + null + "," + null + "," + getLogin().getUserName());
                        n450Store.updateN450i(groupEmployee, Util.parseInt(listIdEmployee.get(i)), null, null, null, null, getLogin().getUserName());
                        System.out.println("Insert moi vo: " + listIdEmployee.get(i));
                    } catch (Exception ex) {
                        System.out.println("Error when insert employee with Id= " + listIdEmployee.get(i) + " and groupEmployee="
                                + groupEmployee + ". ERROR CODE: " + ex.getMessage());
                    }
                }
            }
        }
    }

    /**
     * @return the pn450
     */
    public int getPn450() {
        return pn450;
    }

    /**
     * @param pn450 the pn450 to set
     */
    public void setPn450(int pn450) {
        this.pn450 = pn450;
    }

    /**
     * @return the pv451
     */
    public String getPv451() {
        return pv451;
    }

    /**
     * @param pv451 the pv451 to set
     */
    public void setPv451(String pv451) {
        this.pv451 = pv451;
    }

    /**
     * @return the pv452
     */
    public String getPv452() {
        return pv452;
    }

    /**
     * @param pv452 the pv452 to set
     */
    public void setPv452(String pv452) {
        this.pv452 = pv452;
    }

    /**
     * @return the pv453
     */
    public String getPv453() {
        return pv453;
    }

    /**
     * @param pv453 the pv453 to set
     */
    public void setPv453(String pv453) {
        this.pv453 = pv453;
    }

    /**
     * @return the nv852
     */
    public String getNv852() {
        return nv852;
    }

    /**
     * @param nv852 the nv852 to set
     */
    public void setNv852(String nv852) {
        this.nv852 = nv852;
    }

    /**
     * @return the nv303
     */
    public String getNv303() {
        return nv303;
    }

    /**
     * @param nv303 the nv303 to set
     */
    public void setNv303(String nv303) {
        this.nv303 = nv303;
    }

    /**
     * @return the nd304
     */
    public Date getNd304() {
        return nd304;
    }

    /**
     * @param nd304 the nd304 to set
     */
    public void setNd304(Date nd304) {
        this.nd304 = nd304;
    }

    /**
     * @return the nd305
     */
    public Date getNd305() {
        return nd305;
    }

    /**
     * @param nd305 the nd305 to set
     */
    public void setNd305(Date nd305) {
        this.nd305 = nd305;
    }
}
